package com.itheima.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.controller.utils.Yzm;
import com.itheima.domain.Person;
import com.itheima.domain.Student;
import lombok.val;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;
import java.nio.charset.StandardCharsets;

@SpringBootTest
public class StudentDaoTestCase {

    @Autowired
    private StudentDao bookDao;

    @Test
    void testGetById(){
        System.out.println(bookDao.selectById(1));
    }

    @Test
    void testSave(){
        Student student = new Student();
        student.setType("测试数据123");
        student.setName("测试数据123");
        student.setDescription("测试数据123");
        bookDao.insert(student);
    }

    @Autowired Login login;

    @Test
     void b( ){
        LambdaQueryWrapper wrapper = new LambdaQueryWrapper<Person>().eq(Person::getPassword,123).eq(Person::getUsername,1234);
        Person person = login.selectOne(wrapper);

    }
    @Test
    void a() throws Exception {
        File file = new File("src/main/resources/static/yzm.jpg");
        String s = Yzm.CaptchaUtil.generateVerifyCode(4);


        System.out.println(s);
        Yzm.CaptchaUtil.outputVerifyImage(250, 100, file, 4);


    }

    @Test
    void testUpdate(){
        Student student = new Student();
        student.setId(17);
        student.setType("测试数据abcdefg");
        student.setName("测试数据123");
        student.setDescription("测试数据123");
        bookDao.updateById(student);
    }

    @Test
    void testDelete(){
        bookDao.deleteById(16);
    }

    @Test
    void testGetAll(){
        bookDao.selectList(null);
    }

    @Test
    void testGetPage(){
        IPage page = new Page(2,5);
        bookDao.selectPage(page, null);
        System.out.println(page.getCurrent());
        System.out.println(page.getSize());
        System.out.println(page.getTotal());
        System.out.println(page.getPages());
        System.out.println(page.getRecords());
    }

    @Test
    void testGetBy(){
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.like("name","Spring");
        bookDao.selectList(qw);
    }

    @Test
    void testGetBy2(){
        String name = "1";
        LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper<Student>();
        //if(name != null) lqw.like(Student::getName,name);
        lqw.like(name != null, Student::getName,name);
        bookDao.selectList(lqw);
    }

}










